package com.library.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.library.dto.pojo.PageBean;
import com.library.dto.vo.Books;
import com.library.entity.BorrowInfo;
import com.library.entity.Reader;
import com.library.service.IBooksService;
import com.library.service.IBorrowInfoService;
import com.library.service.serviceImpl.PageServiceImpl;

@Controller
@RequestMapping("/reader")
public class BooksController {

	@Autowired
	private IBooksService booksService;
	
	@Autowired
	private IBorrowInfoService borrow;

	@RequestMapping("/books/{pageNum}")
	public String getBooks(Model model, @PathVariable(value = "pageNum") int pageNum,HttpSession session) {
		int pageSize = 5;
		int count = booksService.selectCount(null);
		Page<Books> selectPage = booksService.selectPage(new Page<Books>(pageNum, pageSize), null);
		List<Books> list = selectPage.getRecords();
		PageBean<Books> pageBean = new PageServiceImpl().page(list, pageNum, pageSize, count);
		if (pageNum > pageBean.getTotalPage()) {
			pageNum = pageBean.getTotalPage();
			pageBean.setPageNum(pageNum);
			selectPage = booksService.selectPage(new Page<Books>(pageNum, pageSize), null);
			list = selectPage.getRecords();
			pageBean = new PageServiceImpl().page(list, pageNum, pageSize, count);
		}
		model.addAttribute("bean", pageBean);
		model.addAttribute("select", false);
		Reader reader = (Reader)session.getAttribute("reader");
		EntityWrapper<BorrowInfo> wrapper = new EntityWrapper<BorrowInfo>();
		wrapper.in("readerId",String.valueOf(reader.getReaderId()));
		List<BorrowInfo> selectList = borrow.selectList(wrapper);
		for (BorrowInfo borrowInfo : selectList) {
			if(borrowInfo.getPenalty()>0) {
				session.setAttribute("borrow", false);
			}else {
				session.setAttribute("borrow", true);
			}
		}
		return "reader/readerselect";
	}

	@RequestMapping("/books/select")
	public String selectBooks( @RequestParam String isbn,Model model,
			@RequestParam String author, @RequestParam String publisher, @RequestParam String typeName,
			@RequestParam String bookName,HttpSession session) {
		int pageNum = 1;
		EntityWrapper<Books> entityWrapper = new EntityWrapper<Books>();
		if(author!=null) {
			entityWrapper.like("author", author);
		}
		if(publisher!=null) {
			entityWrapper.like("publisher", publisher);
		}
		if(bookName!=null) {
			entityWrapper.like("bookName", bookName);
		}
		if(isbn!=null && !isbn.equals("")) {
			entityWrapper.eq("isbn", isbn);
		}
		if(typeName!=null && !typeName.equals("")) {
			entityWrapper.eq("typeName", typeName);
		}
		int pageSize = 5;
		int count = booksService.selectCount(entityWrapper);
		session.setAttribute("wrapper", entityWrapper);
		Page<Books> selectPage = booksService.selectPage(new Page<Books>(pageNum, pageSize), entityWrapper);
		List<Books> list = selectPage.getRecords();
		PageBean<Books> pageBean = new PageServiceImpl().page(list, pageNum, pageSize, count);
		model.addAttribute("bean", pageBean);
		model.addAttribute("select", true);
		Reader reader = (Reader)session.getAttribute("reader");
		EntityWrapper<BorrowInfo> wrapper = new EntityWrapper<BorrowInfo>();
		wrapper.in("readerId",String.valueOf(reader.getReaderId()));
		List<BorrowInfo> selectList = borrow.selectList(wrapper);
		for (BorrowInfo borrowInfo : selectList) {
			if(borrowInfo.getPenalty()>0) {
				session.setAttribute("borrow", false);
			}else {
				session.setAttribute("borrow", true);
			}
		}
		return "reader/readerselect";
	}
	
	@RequestMapping("/books/select/{pageNum}")
	public String booksSelect(Model model,HttpSession session,@PathVariable(value = "pageNum") int pageNum) {
		int pageSize = 5;
		EntityWrapper<Books> wrapper = (EntityWrapper<Books>) session.getAttribute("wrapper");
		int count = booksService.selectCount(wrapper);
		Page<Books> selectPage = booksService.selectPage(new Page<Books>(pageNum, pageSize), wrapper);
		List<Books> list = selectPage.getRecords();
		PageBean<Books> pageBean = new PageServiceImpl().page(list, pageNum, pageSize, count);
		model.addAttribute("bean", pageBean);
		model.addAttribute("select", true);
		Reader reader = (Reader)session.getAttribute("reader");
		EntityWrapper<BorrowInfo> wrapper1 = new EntityWrapper<BorrowInfo>();
		wrapper.in("readerId",String.valueOf(reader.getReaderId()));
		List<BorrowInfo> selectList = borrow.selectList(wrapper1);
		for (BorrowInfo borrowInfo : selectList) {
			if(borrowInfo.getPenalty()>0) {
				session.setAttribute("borrow", false);
			}else {
				session.setAttribute("borrow", true);
			}
		}
		return "reader/readerselect";
	}
}
